﻿//创建时间: 2011-4-14
//创建作者: 李 城
//功能说明: 发布引擎的基类
//最后修改: 2011-4-14 

#define PrintCMSVersion

using System;
using System.Collections.Generic;
using System.Text;

using TRPOP.Common;
using TRPOP.CMS.Template;
using TRPOP.CMS.Model;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using TRPOP.CMS.Business;
using TRPOP.Config;
using TRPOP.CMS.Plugin;
using System.Threading;

namespace TRPOP.CMS.Publish
{
    public class PubBase
    {

        //public const string mStrVersion = "<!-- PagePublishInfo  [  PublishTime: {0} ; Publisher: UserID__{1}、UserName__{2}  ]  Powered by [ YorkCMS V1.0 Released ] --->"; 
        public static string mStrVersion
        {
            get
            {
                return "<!-- PagePublishInfo  [  Published at  {0} , Published by  {1} , CurrentThreadId is {2} ]  Powered by [ " + TRPOP.CMS.Plugin.VersionLimit.VersionLevel + " ] --->";
            }
        }

        /// <summary>
        /// 获取CMS版本信息
        /// </summary>
        public static string getCMSVersion
        {
            get
            {
                int UserId = 0;
                string UserName = "NullName";
                TRPOP.CMS.UserManage.AdminUserInfo oUserInfo = null;
                try
                {
                    oUserInfo = IAdminUserAccess.GetInstance().getUserBaseInfo() as TRPOP.CMS.UserManage.AdminUserInfo;
                }
                catch { }
                if (oUserInfo == null)
                {
                    object objUserID = Thread.GetData(Thread.GetNamedDataSlot("CurrentUserID"));
                    if (objUserID != null)
                    {
                        int.TryParse(objUserID.ToString(), out UserId);
                        if (UserId > 0)
                        {
                            oUserInfo = IAdminUserAccess.GetInstance().getUserBaseInfo(UserId) as TRPOP.CMS.UserManage.AdminUserInfo;
                        }
                    }
                }
                if(oUserInfo!=null)
                {
                    UserId = oUserInfo.UserBaseId;
                    UserName = oUserInfo.Name;
                }
                //return (Environment.NewLine + string.Format(mStrVersion, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), UserId, UserName));
                return (Environment.NewLine + string.Format(mStrVersion, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), UserName, Thread.CurrentThread.ManagedThreadId));
            }
        }

        /// <summary>
        /// 根据结点实体获取所指定的字符替换
        /// </summary>
        /// <param name="oNodeInfo"></param>
        /// <returns></returns>
        public static List<WordReplaceInfo> GetReplaceList(NodeInfo oNodeInfo)
        {
            return new WordReplaceManage().GetListByCondition(0, " and Nodeids like '%|" + oNodeInfo.NodeId.ToString() + "|%' ", "");
        }

        /// <summary>
        /// 根据指定字符串或正则表达式 来替换字符
        /// </summary>
        /// <param name="strHtml"></param>
        /// <param name="strold">可以是正则表达式</param>
        /// <param name="strnew"></param>
        public static string WordReplaceInfo(string strHtml, string strold, string strnew)
        {
            if (strHtml.IsEmpty() || strold.IsEmpty())
                return strHtml;
            try
            {
                return Regex.Replace(strHtml, strold, strnew, RegexOptions.IgnoreCase | RegexOptions.Multiline);
            }
            catch
            {
                return strHtml.Replace(strold, strnew);
            }
        }

        /// <summary>
        /// 根据ListWordReplaceInfo 来替换字符
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="olist"></param>
        public static void WordReplaceForPub(ref object obj, List<WordReplaceInfo> olist)
        {
            if (olist == null || olist.Count <= 0)
                return;
            else if (obj is string)
            {
                foreach (WordReplaceInfo oinfo in olist)
                {
                    obj = WordReplaceInfo(obj.ToString(), oinfo.OldValue, oinfo.NewValue);
                }
            }
            else if (obj is ArrayList)
            {
                ArrayList oArray = obj as ArrayList;
                for (int i = 0; i < oArray.Count; i++)
                {
                    foreach (WordReplaceInfo oinfo in olist)
                    {
                        oArray[i] = WordReplaceInfo(oArray[i].ToString(), oinfo.OldValue, oinfo.NewValue);
                    }
                }
            }
        }

        /// <summary>
        /// 根据 字符串数组、替换后的字符 来替换字符
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="strNewWords">替换后的字符</param>
        /// <param name="oStrArray">要替换的字符数组</param>
        public static void WordReplaceForPub(ref object obj, string strNewWords, string[] oStrArray)
        {
            if (obj is string)
            {
                foreach (string strTemp in oStrArray)
                {
                    obj = WordReplaceInfo(obj.ToString(), strTemp, strNewWords);
                }
            }
            else if (obj is ArrayList)
            {
                ArrayList oArray = obj as ArrayList;
                for (int i = 0; i < oArray.Count; i++)
                {
                    foreach (string strTemp in oStrArray)
                    {
                        oArray[i] = WordReplaceInfo(oArray[i].ToString(), strTemp, strNewWords);
                    }
                }
            }
        }

        /// <summary>
        /// 生成页面
        /// </summary>
        /// <param name="PageHtmlobj">htmlobject，可以为string 也可以为arraylist</param>
        /// <param name="strRoot">生成页面的目录</param>
        /// <param name="strFileName">文件名 例如:index </param>
        /// <param name="PageExtName">文件后缀 例如.html </param>
        /// <param name="intIndexPage">起始页</param>
        public static void PubPages(object PageHtmlobj, string strRoot, string strFileName, string PageExtName, int intIndexPage)
        {
            if (strRoot.IsEmpty() || strFileName.IsEmpty() || PageExtName.IsEmpty())
                return;
            if (PageHtmlobj is string)
            {
                FileUtil.WriteFile(Common.Util.GetMapPath(strRoot + strFileName + ContentPage.GetPageNameNum(intIndexPage) + PageExtName),
                                    GetCompressHtml(SaveCMSVersion(PageHtmlobj.ToString()))
                                   );
            }
            else if (PageHtmlobj is ArrayList)
            {
                ArrayList oList = PageHtmlobj as ArrayList;
                for (int i = 1; i <= oList.Count; i++)
                {
                    FileUtil.WriteFile(Common.Util.GetMapPath(strRoot + strFileName + ContentPage.GetPageNameNum(i + intIndexPage - 1) + PageExtName),
                                       GetCompressHtml(SaveCMSVersion(oList[i - 1].ToString()))
                                      );
                }
            }
        }

        /// <summary>
        /// 对生成的HTML进行压缩处理
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string GetCompressHtml(string strHtml)
        {
            return  TRPOP.Common.CommonRegex.CutHtmlRedundanceSpace(strHtml);
        }
        /// <summary>
        /// 将CMSversion及发布页面信息添加到html代码里
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string SaveCMSVersion(string strSource)
        {
#if PrintCMSVersion
            int intIndexOfBody = strSource.LastIndexOf(@"</body>");
            if (intIndexOfBody > 0)
            {
                return strSource.Insert(intIndexOfBody, getCMSVersion + System.Environment.NewLine);
            }
#endif
            return strSource;
        }
    }
}